package com.platform.common.utils.wx;

import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @description 获取ticket
 */
public class JsapiTicketUtil {
    public static String JsapiTicket(String accessToken) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("type", "jsapi");
        params.put("access_token", accessToken);
        ResponseEntity<String> reponseEntity = restTemplate.getForEntity(url+"?type={type}&access_token={access_token}", String.class, params);
        String access_ticketStr = reponseEntity.getBody();
        Map<String, String> result = new HashMap<String, String>();
        try {
            JSONObject jsonObject = new JSONObject(access_ticketStr);
            /*String errcode = String.valueOf(jsonObject.get("errcode"));
            String errmsg = String.valueOf(jsonObject.get("errmsg"));
            String expires_in = String.valueOf(jsonObject.get("expires_in"));*/
            String ticket = String.valueOf(jsonObject.get("ticket"));
            return ticket;
        }catch (JSONException e) {}
        return "";
    }
}
